package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.adpater.pingxx.domain.PayDO;
import com.xyy.saas.payment.callback.core.handler.PaySuccessHandler;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.dao.model.PrePayOrder;
import com.xyy.saas.payment.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 *测试
 */
@RestController
@RequestMapping(value = {"/api/test"})
public class TestPaymentController {
    private static Logger LOGGER = LoggerFactory.getLogger(TestPaymentController.class);
    @Autowired
    OrderService orderService;
    @Autowired
    private PaySuccessHandler paySuccessHandler;

    @RequestMapping(value = {"/testPay"}, method = {RequestMethod.GET})
    public String testPay(@RequestParam String payNo, @RequestParam String channel) {
        LOGGER.info("AccountController testPay begin payNo:{}, channel:{}",payNo,channel);
        ResultVO resultVO = new ResultVO();
        //根据订单号获取订单信息
        PrePayOrder po = orderService.getPrePayOrderByPayNo(payNo);
        if(po.getPayNo()==null){
            LOGGER.error("订单不存在 payNo:{}",payNo);
            return "订单不存在";
        }
        PayDO domain = new PayDO();//根据订单生成 PayDO对象
        domain.setOrderNo(payNo);
        domain.setChannel(Constants.CHANNEL_AGGREGATE_NAME+"_"+channel);
        domain.setChannelChargeNo("mock"+payNo);
        domain.setAmount(po.getAmount());//设置支付金额等于订单金额
        domain.setTimePaid(new Date());
        if(po.getStatus()==1){
            domain.setResultCode("SUCCESS");//通知支付成功
            domain.setReturnCode("SUCCESS");
        }
        paySuccessHandler.callBackHandler(domain);//把PayDO对象传给PingXXPaySuccessHandler
        LOGGER.info("AccountController testPay end payNo:{}, channel:{}",payNo,channel);
        return "SUCCESS";
    }

}
